<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" :model="filters" class="demo-form-inline" ref="searchForm">
            <el-form-item prop="equipName">
              <el-input v-model="filters.equipName" clearable placeholder="设备名称"></el-input>
            </el-form-item>
            <el-form-item prop="equipNickName">
              <el-input v-model="filters.equipNickName" clearable placeholder="设备昵称"></el-input>
            </el-form-item>
            <el-form-item prop="equipCode">
              <el-input v-model="filters.equipCode" clearable placeholder="设备编号"></el-input>
            </el-form-item>
            <el-form-item prop="equipMac">
              <el-input v-model="filters.equipMac" clearable placeholder="MAC地址"></el-input>
            </el-form-item>
            <el-form-item prop="errorState">
              <el-select placeholder="报警状态" v-model="filters.errorState" clearable filterable>
                <el-option :value="0" label="正常"></el-option>
                <el-option :value="1" label="报警"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="equipType">
              <el-select placeholder="设备类型" v-model="filters.equipType" clearable filterable>
                <el-option :value="1" label="呼叫器"></el-option>
                <el-option :value="2" label="接收器"></el-option>
                <el-option :value="3" label="安全设备"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="equipState">
              <el-select placeholder="设备状态" v-model="filters.equipState" clearable filterable>
                <el-option :value="1" label="待连接"></el-option>
                <el-option :value="2" label="在线"></el-option>
                <el-option :value="3" label="离线"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="onSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
          <div>
          </div>
        </div>
      </el-col>
    </el-row>

    <el-row ref="tableRow">
      <el-table :data="dataList" :fit="true" highlight-current-row v-loading="listLoading"
        element-loading-text="数据加载中，请稍后..." empty-text="对不起，没有找到相关数据">
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column width="200" label="设备名称" align="center" prop="equipName"> </el-table-column>
        <el-table-column width="200" label="设备昵称" align="center" prop="equipNickName"> </el-table-column>
        <el-table-column width="200" label="设备编号" align="center" prop="equipCode"> </el-table-column>
        <el-table-column align="center" label="MAC地址" width="200" prop="equipMac"> </el-table-column>
        <el-table-column align="center" label="设备卡号（ICCID）" width="200" prop="iccid"> </el-table-column>
        <el-table-column align="center" width="200" label="报警状态" prop="errorState">
          <template slot-scope="scope">
            {{ scope.row.errorState == 0 ? '正常' : '报警' }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="设备状态" width="110px">
          <template slot-scope="scope">
            <el-tag size="mini" effect="dark" :type="formatReceiverStates(scope.row.equipState)">
              {{ formatReceiverState(scope.row.equipState) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" width="200" label="设备类型" prop="equipType">
          <template slot-scope="scope">
            {{ scope.row.equipType == 1 ? '呼叫器' : scope.row.equipType == 2 ? '接收器' : '安全设备' }}
          </template>
        </el-table-column>
        <el-table-column align="center" width="200" label="创建时间" prop="createDate"> </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="200px">
          <template slot-scope="scope">
            <el-dropdown trigger="click" size="small">
              <el-button type="primary"> <i class="fa fa-list-ul"></i>更多 </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item v-has="permissionTransfer"
                  @click.native="handleClickTransfer(scope.row)">转移租户</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="page.pageNo" :page-sizes="[20, 50, 100]" :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev" :total="page.pageTotal"></el-pagination>
    </el-row>
    <el-dialog width="30%" title="选择租户" :visible.sync="copyShow" @close="cancelCopyLessee">
      <div>
        <el-form>
          <el-form-item required label="租户">
            <el-select v-model="lesseeInfo.lesseeId" clearable placeholder="请选择租户">
              <el-option v-for="item in lesseeOptions" :key="item.value" :label="item.name" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer">
        <el-button type="primary" @click="copyLessee">
          提交
        </el-button>
        <el-button @click="cancelCopyLessee">
          取消
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import AllEquipApi from '@/api/equip/allEquip';
import BusinessApi from '@/api/business/business';
import * as message from '@/constants/message';
export default {
  name: 'allEquipManagement',
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      dataList: [],
      listLoading: true,
      filters: {
        equipName: '',
        equipNickName: '',
        equipCode: '',
        equipMac: '',
        errorState: '',
        equipType: '',
        equipState:''
      },
      page: {
        pageNo: 1,
        pageTotal: 0,
        pageSize: 20,
      },
      copyShow: false,
      lesseeOptions: [],
      lesseeInfo: {
        lesseeId: '',
      },
      equipId:0,
      // 权限
      permissionTransfer: 'SYSTEM_MANAGER_EQUIP_ALLEQUIP_LIST_TRANSFER',
    };
  },
  mounted() {
    this.init();
    window.addEventListener('keydown', this.keyDown);
  },
  activated() {
    this.listData();
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  methods: {
    getLesseeList() {
      BusinessApi.lesseeList({ filter: 1 }).then(res => {
        if (res.status == 0) {
          this.lesseeOptions = res.data
        }
      })
    },
    init() {
      let _this = this;
      _this.listLoading = true;
      _this.listData();
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode == 13) {
        this.onSearch();
      }
    },

    handleSizeChange(size) {
      let _this = this;
      _this.page.pageSize = size;
      _this.listData();
    },
    handleCurrentChange(val) {
      let _this = this;
      _this.page.pageNo = val;
      _this.listData();
    },
    formatReceiverState(cellValue) {
      if (cellValue === 0) {
        return '未开通';
      } else if (cellValue === 1) {
        return '未激活';
      } else if (cellValue === 2) {
        return '在线';
      } else if (cellValue == 3) {
        return '离线';
      }
    },
    formatReceiverStates(cellValue) {
      if (cellValue === 0) {
        return 'danger';
      } else if (cellValue === 1) {
        return '';
      } else if (cellValue === 2) {
        return 'success';
      } else if (cellValue === 3) {
        return 'info';
      }
    },
    resetForm() {
      this.$refs.searchForm.resetFields();
      this.onSearch();
    },
    onSearch() {
      this.listLoading = true;
      this.page.pageNo = 1;
      this.listData();
    },
    listData() {
      let _this = this;
      var cond = {
        pageNo: _this.page.pageNo,
        pageSize: _this.page.pageSize,
        equipName: _this.filters.equipName,
        equipNickName: _this.filters.equipNickName,
        equipCode: _this.filters.equipCode,
        equipMac: _this.filters.equipMac,
        errorState: _this.filters.errorState,
        equipState: _this.filters.equipState,
        equipType: _this.filters.equipType,
      };
      // 发送请求
      AllEquipApi.getAllEquip(cond)
        .then((res) => {
          _this.listLoading = false;
          _this.dataList = res.data.list;
          _this.page.pageTotal = res.data.total;
        })
        .catch((err) => {
          _this.listLoading = false;
        });
    },
    handleClickTransfer(val) {
      //编辑 进入新增页通过id来查找数据
      this.equipId = val.equipId
      this.getLesseeList()
      this.copyShow = true
    },
    cancelCopyLessee() {
      this.copyShow = false
      this.lesseeInfo = {
        lesseeId: '',
      }
      this.lesseeOptions = []
    },
    copyLessee() {
      if(!this.lesseeInfo.lesseeId){
        this.$message.error("请选择租户")
        return false
      }
      let params = {
        equipId:this.equipId,
        tenantId: this.lesseeInfo.lesseeId
      }
      this.$confirm("确认转移该设备至新租户吗？", "提示", {}).then(() => {
        AllEquipApi.transferTenant(params).then(res => {
          if(res.status==0){
            this.$Utils.notifySuccess("操作成功")
            this.listData();
          }
          this.cancelCopyLessee()
        }).finally(() => {
        })
      })
    },
  },
};
</script>

<style lang="scss">
@import '@/assets/css/views/content.scss';
</style>
